
CROSS_COMPILE = loongarch64-linux-gnu-

CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
AR = $(CROSS_COMPILE)ar
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size

INCLUDES := -Iinclude -Iclib

### 
CCFLAGS := -static -G0 -pipe -Wa,-mla-global-with-pcrel -g -O2

LDFLAGS := -Tlinker.script -nostartfiles -Map=link.map -static -G0 

# LDFLAGS += --strip-all
LDFLAGS += -e 0x102000000

# --gc-sections, we ignored all "Unnecessary* sections and symbols
# -u instead of all symbol are checked out 
# LDFLAGS += --gc-sections -u _start #

##-----------------------------------------------------##
TEST_CASE = coremark#
TEST_CASE = hello_world#
##-----------------------------------------------------##

all: $(TEST_CASE)

# Generate libla.a
LA_LIB := libla.a

LIB_NEWLIB_SRC := printf.c vasprintf.c #__thead_printf.c 
LIB_NEWLIB_SRC := $(addprefix newlib_wrap/, $(LIB_NEWLIB_SRC))
LIB_NEWLIB_OBJ := $(addsuffix .o, $(basename $(LIB_NEWLIB_SRC)))

LIB_CLIB_SRC := fputc.c
LIB_CLIB_SRC := $(addprefix clib/, $(LIB_CLIB_SRC))
LIB_CLIB_OBJ := $(addsuffix .o, $(basename $(LIB_CLIB_SRC)))

# $(info $(LIB_NEWLIB_OBJ))
# $(info $(LIB_CLIB_OBJ))

$(LIB_NEWLIB_OBJ): %.o : %.c
	$(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@

$(LIB_CLIB_OBJ): %.o : %.c
	$(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@

LIB_OBJ := $(LIB_NEWLIB_OBJ) $(LIB_CLIB_OBJ)

lib_obj: $(LIB_NEWLIB_OBJ) $(LIB_CLIB_OBJ)
	$(AR) -r $(LA_LIB) $(LIB_OBJ)

crt:
	$(CC) $(CCFLAGS) -c crt/start.S -o crt/start.o


# TEST Hello World
TEST_APP0_SRC := hello_world.c
TEST_APP0_SRC := $(addprefix hello_world/, $(TEST_APP0_SRC))
TEST_APP0_OBJ := $(addsuffix .o, $(basename $(TEST_APP0_SRC)))

$(TEST_APP0_OBJ): %.o : %.c
	$(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@

hello_world.exe: lib_obj crt $(TEST_APP0_OBJ) 
	$(LD) $(LDFLAGS) -L. crt/start.o $(TEST_APP0_OBJ) -o $@ --start-group -lla --end-group
	$(OBJDUMP) -alD $@ > $@.S
	@echo "ELF SIZE DUMP:"
	@$(SIZE) $@ | tee $@.size


# TEST CoreMark
TEST_APP1_SRC := core_list_join.c core_util.c core_matrix.c \
                 core_portme.c core_state.c core_main.c

TEST_APP1_SRC := $(addprefix coremark/, $(TEST_APP1_SRC))
TEST_APP1_OBJ := $(addsuffix .o, $(basename $(TEST_APP1_SRC)))

$(TEST_APP1_OBJ): %.o : %.c
	$(CC) $(CCFLAGS) $(INCLUDES) -c $< -o $@

coremark.exe: lib_obj crt $(TEST_APP1_OBJ) 
	$(LD) $(LDFLAGS) -L. crt/start.o $(TEST_APP1_OBJ) -o $@ --start-group -lla --end-group
	$(OBJDUMP) -alD $@ > $@.S
	@echo "ELF SIZE DUMP:"
	@$(SIZE) $@ | tee $@.size


$(TEST_CASE).hex: $(TEST_CASE).exe
	$(OBJCOPY) -O binary $< $@
	$(OBJCOPY) -O srec $< $@.text -j .text*  -j .rodata* -j .eh_frame*
	$(OBJCOPY) -O srec $< $@.data -j .data*  -j .bss -j .COMMON

$(TEST_CASE): $(TEST_CASE).hex
	bin/Srec2vmem $(TEST_CASE).hex.text inst.pat
	bin/Srec2vmem $(TEST_CASE).hex.data data.pat


.PHONY: hello_world crt

clean:
	rm -rf $(LIB_OBJ) $(TEST_APP0_OBJ) $(LA_LIB) *.exe* link.map \
	*.hex* *.pat
